define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'layer'], function ($, undefined, Backend, Table, Form, Layer) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilumarket/discover/topic/index' + location.search,
                    add_url: 'xilumarket/discover/topic/add',
                    edit_url: 'xilumarket/discover/topic/edit',
                    del_url: 'xilumarket/discover/topic/del',
                    multi_url: 'xilumarket/discover/topic/multi',
                    multiAdd_url: 'xilumarket/discover/topic/multi_add',
                    users_url: 'xilumarket/discover/topic/users',
                    dragsort_url:'',
                    table: 'xilumarket_topic',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                commonSearch: true,
                escape:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE',align:'left',formatter: Controller.api.formatter.title},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'description', title: __('Description'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'),yes:'normal',no:'hidden', searchList: {"hidden":__('Status hidden'),"normal":__('Status normal')}, formatter: Table.api.formatter.normal},
                        {field: 'top_status', title: __('Top_status'), searchList: {"0":__('Top_status 0'),"1":__('Top_status 1')}, formatter: Table.api.formatter.normal},
                        {field: 'forum_count', title: __('Forum_count'), operate: false},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons:[
                            {
                                name: 'detail',
                                text: __('users'),
                                title: __('users'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-user',
                                url: 'xilumarket/discover/topic/users',
                                callback: function (data) {
                                    Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                },
                                visible: function (row) {
                                    //返回true时按钮显示,返回false隐藏
                                    return true;
                                }
                            },
                        ]},
                    ]
                ]
            });
            $(document).on("click", ".btn-multiAdd", function (e) {
              // Fast.api.open($.fn.bootstrapTable.defaults.extend.multiAdd_url)
              Layer.open({
                title: '批量增加',
                area: ['800px', '400px'],
                type: 2, 
                content: Fast.api.cdnurl('/admin/xilumarket/discover/topic/multi_add'),
                yes: function(index, layero, that){
                  Layer.close(index)
                }
              })
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        multi_add: function () {//待处理
          $('.btn-multi-sure').on('click',function() {
            Fast.api.ajax({ url: "xilumarket/discover/topic/multi_add", data: {
              'key': $('#c-multi-title').val()
            }}, function (data) {
              Fast.api.close()
              $('#table').bootstrapTable('refresh', {silent: true})
            });
          })
        },
        users: function () {
            Table.api.init({
                extend: {
                    users_url: 'xilumarket/discover/topic/users',
                }
            });
            var utable = $("#utable");
            var id = $("#utable").data('operateUser');
            // 初始化表格
            utable.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.users_url + '/ids/' + id,
                pk: 'id',
                sortName: 'is_admin',
                sortOrder: 'desc',
                // fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                // commonSearch: true,
                escape:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'user.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'user.avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'user.gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'createtime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'is_admin_text', title: __('Admin')},
                        {field: 'operate', title: __('Operate'),table: utable, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [{
                            name: 'setting_topic_admin',
                            text: '指定',
                            title: __('Settings'),
                            refresh: true,
                            classname: 'btn btn-xs btn-primary btn-ajax',
                            icon: 'fa fa-user',
                            url: 'xilumarket/discover/topic/setTopicAdmin/id/{ids}',
                            callback: function (data) {
                              Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                            },
                            visible: function (row) {
                              //返回true时按钮显示,返回false隐藏
                              if (row.is_admin == '11') {
                                return false
                              }
                              console.log(row);
                              return true;
                            }
                        }]}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(utable);
        },
        api: {
            formatter: {
                title: function (value, row, index) {
                    return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
